package message.push.client;

import java.io.IOException;
import java.util.UUID;

import message.push.util.Aes;
import message.push.util.TimeProcesser;

import org.jboss.netty.buffer.ChannelBuffer;
import org.jboss.netty.buffer.ChannelBufferOutputStream;
import org.jboss.netty.buffer.ChannelBuffers;

import com.cndw.rpg.net.SocketClient;

import flex.messaging.io.amf.Amf3Output;

public class MySocketRequest{
	
	private String requestId;
	private Object parameters[];
	private String controllerName;
	private String controllerMethod;
	private int connetTimeOut;
	private int readTimeOut;

	public MySocketRequest(String controllerName, String controllerMethod, Object... parameters) {
		connetTimeOut = 3000;
		readTimeOut = 10000;
		this.parameters = parameters;
		requestId = UUID.randomUUID().toString();
		this.controllerName = controllerName;
		this.controllerMethod = controllerMethod;
	}

	protected ChannelBuffer setRequestHeader(ChannelBuffer buffer) {
		ChannelBuffer request = ChannelBuffers
				.buffer(buffer.readableBytes() + 8);
		request.writeBytes(new byte[] { 99, 110, 100, 119 });
		request.writeInt(buffer.readableBytes());
		request.writeBytes(buffer);
		return request;
	}

	public ChannelBuffer serialize() {
		ChannelBuffer buffer = ChannelBuffers.dynamicBuffer();
		ChannelBufferOutputStream outputStream = new ChannelBufferOutputStream(buffer);
		Amf3Output output = new Amf3Output(SocketClient.context);
		output.setOutputStream(outputStream);
		try {
			output.writeObject(controllerName);
			output.writeObject(controllerMethod);
			output.writeObject(requestId);
//			output.writeObject(getCipher());
			if (parameters != null) {
				Object aobj[];
				int j = (aobj = parameters).length;
				for (int i = 0; i < j; i++) {
					Object parameter = aobj[i];
					output.writeObject(parameter);
				}

			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		return setRequestHeader(buffer);
	}

	public String getRequestId() {
		return requestId;
	}


	public String getControllerName() {
		return controllerName;
	}

	public String getControllerMethod() {
		return controllerMethod;
	}

	public int getConnetTimeOut() {
		return connetTimeOut;
	}

	public void setConnetTimeOut(int connetTimeOut) {
		this.connetTimeOut = connetTimeOut;
	}

	public int getReadTimeOut() {
		return readTimeOut;
	}

	public void setReadTimeOut(int readTimeOut) {
		this.readTimeOut = readTimeOut;
	}
	
	public String getCipher() {
		return encryptRequest(getControllerName(), getControllerMethod());
	}
	
	private static String encryptRequest(String... params) {
		StringBuilder param = new StringBuilder();
		for (String p : params) {
			param.append(p);
			param.append("\r\n");
		}
		param.append(TimeProcesser.getUnixTime());
		return Aes.encrypt(param.toString());
	}

	public String info() {
		StringBuilder sb = new StringBuilder();
		sb.append(" c ").append(controllerName);
		sb.append(" m ").append(controllerMethod);
		return sb.toString();
	}

}